package plus.easydo.starter.log.configuration;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import plus.easydo.starter.log.server.DefaultLogStoreServer;

import javax.sql.DataSource;
import java.util.Objects;

/**
 * 自动注册
 * @author laoyu
 * @version 1.0
 */
@Configuration
public class SystemLogAutoConfiguration {

    Logger logger = LoggerFactory.getLogger(SystemLogAutoConfiguration.class);


    /**
     * 初始化动态数据源，设置默认数据源
     *
     * @param dataSource dataSource
     * @return plus.easydo.jdbc.config.DynamicDataSource
     * @author laoyu
     */
    @Bean(name = "logServerJdbcTemplate")
    public JdbcTemplate jdbcTemplate(@Autowired(required = false) DataSource dataSource){
        if(Objects.isNull(dataSource)){
            logger.warn("没有找到默认数据源");
        }
        return new JdbcTemplate(dataSource);
    }

    @Bean
    @ConditionalOnBean(JdbcTemplate.class)
    public DefaultLogStoreServer logServer(){
        return new DefaultLogStoreServer();
    }


}
